package cn.sinata.xldutils_kotlin.rxutils

import io.reactivex.Flowable
import io.reactivex.processors.FlowableProcessor
import io.reactivex.processors.PublishProcessor

class RxBus private constructor() {
    private val mBus: FlowableProcessor<Any> = PublishProcessor.create<Any>().toSerialized()

    fun post(obj: Any) {
        mBus.onNext(obj)
    }

    fun <T> toFlowable(tClass: Class<T>): Flowable<T> = mBus.ofType(tClass)

    fun toFlowable(): Flowable<Any> = mBus

    fun hasSubscribers(): Boolean = mBus.hasSubscribers()

    companion object {
        fun get() = INSTANCE.rxBus
    }

    private object INSTANCE {
        val rxBus = RxBus()
    }
}